﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CoreRay.Client
{
    /// <summary>
    /// Interaction logic for SettingsWindow.xaml
    /// </summary>
    public partial class SettingsWindow : Window
    {
        public SettingsWindow()
        {
            InitializeComponent();

            this.Settings = CoreRay.Client.Properties.Settings.Default;

            this.Loaded += new RoutedEventHandler(SettingsWindow_Loaded);
        }

        public CoreRay.Client.Properties.Settings Settings
        {
            get;
            private set;
        }

        public RenderSettings RenderSettings
        {
            get;
            private set;
        }

        void SettingsWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //Init render settings
            //foreach(var renderAccelerator in CoreRayContext.Current.Renderer.RenderAccelerators)
            //{
            //    this.renderAcceleratorComboBox.Items.Add(renderAccelerator.ToString());
            //}

            if (this.renderAcceleratorComboBox.Items.Count == 0)
            {
                this.renderAcceleratorCheckBox.IsEnabled = false;
                this.renderAcceleratorComboBox.Items.Add("No render accelerators available");
            }

            //Load settings
            this.resolutionWidthTextBox.Text = this.Settings.ResolutionWidth.ToString();
            this.resolutionHeightTextBox.Text = this.Settings.ResolutionHeight.ToString();
            this.inputTextBox.Text = this.Settings.InputFilename;
            this.outputTextBox.Text = this.Settings.OutputFilename;
        }

        private void resolutionButton_Click(object sender, RoutedEventArgs e)
        {
            switch (((Button)sender).Name)
            {
                case "resolution640Button": 
                    this.resolutionWidthTextBox.Text = "640";
                    this.resolutionHeightTextBox.Text = "480";
                    break;

                case "resolution800Button":
                    this.resolutionWidthTextBox.Text = "800";
                    this.resolutionHeightTextBox.Text = "600";
                    break;

                case "resolution1024Button":
                    this.resolutionWidthTextBox.Text = "1024";
                    this.resolutionHeightTextBox.Text = "768";
                    break;

                case "resolution1280Button":
                    this.resolutionWidthTextBox.Text = "1280";
                    this.resolutionHeightTextBox.Text = "1024";
                    break;

                case "resolution1440Button":
                    this.resolutionWidthTextBox.Text = "1440";
                    this.resolutionHeightTextBox.Text = "900";
                    break;

                case "resolution1600Button":
                    this.resolutionWidthTextBox.Text = "1680";
                    this.resolutionHeightTextBox.Text = "1050";
                    break;

                case "resolution720PButton":
                    this.resolutionWidthTextBox.Text = "1280";
                    this.resolutionHeightTextBox.Text = "720";
                    break;

                case "resolution1080PButton":
                    this.resolutionWidthTextBox.Text = "1920";
                    this.resolutionHeightTextBox.Text = "1080";
                    break;
            }
        }

        private void inputBrowseButton_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();
            openFileDialog.Filter = "Collada File|*.dae";

            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.inputTextBox.Text = openFileDialog.FileName;
            }
        }

        private void outputBrowseButton_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog();
            saveFileDialog.Filter = "Bmp File|*.bmp";

            if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.outputTextBox.Text = saveFileDialog.FileName;
            }
        }

        private void renderButton_Click(object sender, RoutedEventArgs e)
        {
            //Save settings
            this.Settings.ResolutionWidth = int.Parse(this.resolutionWidthTextBox.Text);
            this.Settings.ResolutionHeight = int.Parse(this.resolutionHeightTextBox.Text);
            this.Settings.InputFilename = this.inputTextBox.Text;
            this.Settings.OutputFilename = this.outputTextBox.Text;

            this.Settings.Save();

            //Assign render settings
            this.RenderSettings = new RenderSettings();

            this.RenderSettings.RenderWidth = this.Settings.ResolutionWidth;
            this.RenderSettings.RenderHeight = this.Settings.ResolutionHeight;

            this.DialogResult = true;
            this.Close();
        }

        private void exitButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void renderAcceleratorCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            this.renderAcceleratorComboBox.IsEnabled = this.renderAcceleratorCheckBox.IsChecked.HasValue ? this.renderAcceleratorCheckBox.IsChecked.Value : false;
        }
    }
}
